#include <assert.h>
#include <ctype.h>
#include <locale.h>
#include <errno.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/*
Parameter Details

argc
argument count - initialized to the number of space-separated arguments given to the program from
the command-line as well as the program name itself.

argv
argument vector - initialized to an array of char-pointers (strings) containing the arguments (and the
program name) that was given on the command-line.


*/

void check_command_line_arguments(int argc, char *argv[])
{
    printf("***** check_command_line_arguments *****\n");
    if( argc == 2 )
    {
        printf("The argument supplied is %s\n", argv[1]);
    }
    else if( argc > 2 )
    {
        printf("Too many arguments supplied.\n");
    }
    else
    {
        printf("One argument expected.\n");
    }
}

void print_arguments_and_convert_to_integer(int argc, char *argv[] )
{
    printf("***** print_arguments_and_convert_to_integer *****\n");
    for (int i = 1; i < argc; i++)
    {
        printf("Argument %d is: %s\n", i, argv[i]);
        errno = 0;
        char *p;
        long argument_numValue = strtol(argv[i], &p, 10);

        if (p == argv[i])
        {
            fprintf(stderr, "Argument %d is not a number.\n", i);
        }
        else if ((argument_numValue == LONG_MIN || argument_numValue == LONG_MAX) && errno == ERANGE)
        {
            fprintf(stderr, "Argument %d is out of range.\n", i);
        }
        else
        {
            printf("Argument %d is a number, and the value is: %ld\n", i, argument_numValue);
        }
    }
}

void main(int argc, char *argv[] )
{

    check_command_line_arguments(argc, argv);
    print_arguments_and_convert_to_integer(argc, argv );
    system("pause");
}


/*
main 的两个参数的参数名如下:
    int main( int argc, char *argv[] )
    并不一定这样写，只是约定俗成罢了。但是亦可以写成下面这样:

    int main( int test_argc, char *test_argv[] )


命令行参数是使用 main() 函数参数来处理的，
    其中，argc 是指传入参数的个数，
    argv[] 是一个指针数组，指向传递给程序的每个参数。

argv[0] 存储程序的名称，
argv[1] 是一个指向第一个命令行参数的指针，
*argv[n] 是最后一个参数
*/